﻿#region | Copyright © 2556 |

/*
##########################################################################
	Author By : Siriwat Suananpornpanit
	DateTime : 2556/08/30
##########################################################################
*/

#endregion

using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using DataAccess;
using DataAccess.DBConnections;
using DataAccess.ProxyConnections;
using Microsoft.VisualBasic;
using Oracle.DataAccess.Client;

namespace Web.Business
{
    public partial class UserProfileBal
    {
        #region | Conts |

        public static DataAccessClient dal;//= new DataAccessClient(DATABASE_PROXY.AUTH);

        private static DBConnectionsEngine dbEngine;

        private static OracleConnection Conn;

        private static Hashtable hashtable;

        private static UserProfileBal _dal;

        private System.String sql = "";

        public static UserProfileBal GetInstance()
        {
            /* for Init */
            dal = new DataAccessClient(DATABASE_PROXY.AUTH);

            dbEngine = new DBConnectionsEngine();

            Conn = new DatabaseProxy().GetAuthConnection();

            _dal = new UserProfileBal();

            return _dal;
        }

        #endregion

        #region | Helper |

        internal bool Insert(p_USER_PROFILE obj, out string err)
        {
            bool result = false;
            if (dal.Insert(obj, out err) == DB_RESULT.INCOMPLETED)  result = true; 

            return result;
        }

        internal bool Update(p_USER_PROFILE obj, out string err)
        {
            bool result = false;
            if (dal.Update(obj, out err) == DB_RESULT.INCOMPLETED)  result = true; 

            return result;
        }

        internal bool Delete(p_USER_PROFILE obj, out string err)
        {
            bool result = false;
            if (dal.Delete(obj, out err) == DB_RESULT.INCOMPLETED)  result = true; 

            return result;
        }

        #endregion

        #region | dbEngine |

        public IEnumerable<p_USER_PROFILE> Select(System.Web.UI.Page page, p_USER_PROFILE obj)
        {
            IEnumerable<p_USER_PROFILE> entities = new List<p_USER_PROFILE>();
            try
            {
                System.Text.StringBuilder concast = new System.Text.StringBuilder();

                concast.Append(" WHERE 1=1 ");

                hashtable = new Hashtable();

                if (obj != null)
                {
                    if (!String.IsNullOrEmpty(obj.LANG))
                    {
                        concast.Append(" AND LANG=:LANG ");
                        hashtable.Add(":LANG", obj.LANG);
                    }

                    if (!String.IsNullOrEmpty(obj.COMP_CODE))
                    {
                        concast.Append(" AND COMP_CODE=:COMP_CODE ");
                        hashtable.Add(":COMP_CODE", obj.COMP_CODE);
                    }

                    if (!String.IsNullOrEmpty(obj.EMPNO))
                    {
                        concast.Append(" AND EMPNO=:EMPNO ");
                        hashtable.Add(":EMPNO", obj.EMPNO);
                    }

                    if (!String.IsNullOrEmpty(obj.EMPNAME))
                    {
                        concast.Append(" AND EMPNAME=:EMPNAME ");
                        hashtable.Add(":EMPNAME", obj.EMPNAME);
                    }

                    if (!String.IsNullOrEmpty(obj.PASSWORD))
                    {
                        concast.Append(" AND PASSWORD=:PASSWORD ");
                        hashtable.Add(":PASSWORD", obj.PASSWORD);
                    }

                    if (!String.IsNullOrEmpty(obj.DIVISION))
                    {
                        concast.Append(" AND DIVISION=:DIVISION ");
                        hashtable.Add(":DIVISION", obj.DIVISION);
                    }

                    if (!String.IsNullOrEmpty(obj.EMAIL))
                    {
                        concast.Append(" AND EMAIL=:EMAIL ");
                        hashtable.Add(":EMAIL", obj.EMAIL);
                    }

                    if (!String.IsNullOrEmpty(obj.TEL))
                    {
                        concast.Append(" AND TEL=:TEL ");
                        hashtable.Add(":TEL", obj.TEL);
                    }

                    if (!String.IsNullOrEmpty(obj.STATUS))
                    {
                        concast.Append(" AND STATUS=:STATUS ");
                        hashtable.Add(":STATUS", obj.STATUS);
                    }

                    if (!Information.IsDate(obj.EXPIREDATE))
                    {
                        concast.Append(" AND EXPIREDATE=:EXPIREDATE ");
                        hashtable.Add(":EXPIREDATE", obj.EXPIREDATE);
                    }

                    if (!Information.IsDate(obj.CRDATE))
                    {
                        concast.Append(" AND CRDATE=:CRDATE ");
                        hashtable.Add(":CRDATE", obj.CRDATE);
                    }

                    if (!String.IsNullOrEmpty(obj.CRBY))
                    {
                        concast.Append(" AND CRBY=:CRBY ");
                        hashtable.Add(":CRBY", obj.CRBY);
                    }

                    if (!Information.IsDate(obj.UPDDATE))
                    {
                        concast.Append(" AND UPDDATE=:UPDDATE ");
                        hashtable.Add(":UPDDATE", obj.UPDDATE);
                    }

                    if (!String.IsNullOrEmpty(obj.UPDBY))
                    {
                        concast.Append(" AND UPDBY=:UPDBY ");
                        hashtable.Add(":UPDBY", obj.UPDBY);
                    }

                    if (!String.IsNullOrEmpty(obj.POSITION))
                    {
                        concast.Append(" AND POSITION=:POSITION ");
                        hashtable.Add(":POSITION", obj.POSITION);
                    }

                    if (!String.IsNullOrEmpty(obj.PDAFLAG))
                    {
                        concast.Append(" AND PDAFLAG=:PDAFLAG ");
                        hashtable.Add(":PDAFLAG", obj.PDAFLAG);
                    }

                    if (!String.IsNullOrEmpty(obj.EMPID))
                    {
                        concast.Append(" AND EMPID=:EMPID ");
                        hashtable.Add(":EMPID", obj.EMPID);
                    }

                    if (!String.IsNullOrEmpty(obj.OU_CODE))
                    {
                        concast.Append(" AND OU_CODE=:OU_CODE ");
                        hashtable.Add(":OU_CODE", obj.OU_CODE);
                    }

                }
                sql = System.String.Format(@" SELECT LANG ,COMP_CODE ,EMPNO ,EMPNAME ,PASSWORD ,DIVISION ,EMAIL ,TEL ,STATUS ,EXPIREDATE ,CRDATE ,CRBY ,UPDDATE ,UPDBY ,POSITION ,PDAFLAG ,EMPID ,OU_CODE  FROM USER_PROFILE");

                sql = sql + concast.ToString();

                entities = dbEngine.ToList<p_USER_PROFILE>(sql, hashtable, Conn).AsParallel();

            }
            catch (System.Exception ex)
            {
                Core.Modal.Exception(
                    page: page,
                    message: Core.Exception.ModalException(
                                        method: System.Reflection.MethodBase.GetCurrentMethod(),
                                        ex: ex));
                entities = null;
            }
            return entities;
        }


        #endregion
    }
}